package com.mobi.Survey;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobi.Survey.controller.SurveyPojo;
import com.mobi.Survey.controller.User;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomAdapterSurveys.java
 * <p>
 * This class is sub class of BaseAdapter.java and a standard class. Responsible
 * for creating rows of surveys SurveyListScreen.java uses it
 * 
 */
public class CustomAdapterSurveys extends AbstractAdapterSurveyList implements
		View.OnClickListener

{

	public CustomAdapterSurveys(SurveyListScreen surveyScreen)
	{

		this.surveyScreen = surveyScreen;
		this.mInflater = LayoutInflater.from(surveyScreen);

	}

	@Override
	public int getCount()
	{
		if (null == surveys)
			return 0;
		return surveys.size();
	}

	@Override
	public Object getItem(int position)
	{

		return null;
	}

	@Override
	public long getItemId(int position)
	{

		return 0;
	}

	class ViewHolder
	{

		TextView btnStartSurvey;
		TextView btnSurveyInfo;
		TextView btnAnswer;
		TextView btnSurveyResult;

		ImageView ivSurveyInfo;
		ImageView ivAnswer;
		ImageView ivSurveyResult;
		ImageView ivStart;

		View startSurveyLayout;
		View graphLayout;
		View infoLayout;
		View answerLayout;

	}

	private void setupDetailIcons(SurveyPojo survey, ViewHolder holder)
	{
		if (survey.isAnswered() && null != survey.getTxtBtnCorrectAnswer()
				&& survey.getTxtBtnCorrectAnswer().trim().length() > 0)
		{
			holder.answerLayout.setVisibility(View.VISIBLE);
			holder.btnAnswer.setText(' ' + survey.getTxtBtnCorrectAnswer());
			if (null != User.getInstance().getIconGraph().getBitmap())
			{

				holder.ivAnswer.setImageBitmap(User.getInstance()
						.getIconCorrectAnswer().getBitmap());
			} else
			{

				holder.ivAnswer.setImageDrawable(surveyScreen.getResources()
						.getDrawable(R.drawable.rmma_correct_icon_72x72));

			}

		} else
		{
			holder.answerLayout.setVisibility(View.GONE);

		}

		// ///////////////////////////////////////////
		if (null != survey.getTxtBtnSurveyResult()
				&& survey.getTxtBtnSurveyResult().trim().length() > 0)
		{
			holder.graphLayout.setVisibility(View.VISIBLE);
			holder.btnSurveyResult
					.setText(' ' + survey.getTxtBtnSurveyResult());
			if (null != User.getInstance().getIconCorrectAnswer().getBitmap())
			{

				holder.ivSurveyResult.setImageBitmap(User.getInstance()
						.getIconGraph().getBitmap());
			} else
			{
				holder.ivSurveyResult.setImageDrawable(surveyScreen
						.getResources().getDrawable(R.drawable.rmma_bg_border));
			}

		} else
		{
			holder.graphLayout.setVisibility(View.GONE);
		}
		// ///////////////////////////////////////////
		if (null != survey.getSubscription()
				&& !survey.getSubscription().isNull())
		{
			holder.infoLayout.setVisibility(View.VISIBLE);
			if (null != User.getInstance().getIconInfo().getBitmap())
			{

				holder.ivSurveyInfo.setImageBitmap(User.getInstance()
						.getIconInfo().getBitmap());
			} else
			{
				holder.ivSurveyInfo.setImageDrawable(surveyScreen
						.getResources().getDrawable(
								R.drawable.rmma_info_icon_72x72));
			}
		} else
		{

			holder.infoLayout.setVisibility(View.GONE);
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		final SurveyPojo survey = surveys.get(position);
		final ViewHolder holder;
		if (convertView == null)
		{

			convertView = mInflater.inflate(getLayoutId(), null);

			holder = new ViewHolder();

			holder.btnStartSurvey = (TextView) convertView
					.findViewById(R.id.btnStartSurvey);

			holder.btnSurveyInfo = (TextView) convertView
					.findViewById(R.id.btnSurveyInfo);

			holder.btnAnswer = (TextView) convertView
					.findViewById(R.id.btnAnswer);

			holder.btnSurveyResult = (TextView) convertView
					.findViewById(R.id.btnSurveyResult);

			holder.ivAnswer = (ImageView) convertView
					.findViewById(R.id.ivAnswer);
			holder.ivSurveyInfo = (ImageView) convertView
					.findViewById(R.id.ivInfo);
			holder.ivSurveyResult = (ImageView) convertView
					.findViewById(R.id.ivGraph);
			holder.ivStart = (ImageView) convertView.findViewById(R.id.ivStart);

			holder.startSurveyLayout = convertView
					.findViewById(R.id.startLayout);
			holder.graphLayout = convertView.findViewById(R.id.graphLayout);
			holder.infoLayout = convertView.findViewById(R.id.infoLayout);
			holder.answerLayout = convertView.findViewById(R.id.answerLayout);

			convertView.setTag(holder);

		} else
		{
			holder = (ViewHolder) convertView.getTag();

		}

		holder.btnStartSurvey.setText(' ' + survey.getSurveyDiscription());

//		if (survey.getSubscription() != null
//				&& !survey.getSubscription().isNull())
//		{
//			if (null != survey.getTxtButtonInformation()
//					&& survey.getTxtButtonInformation().trim().length() > 0)
//			{
//				holder.btnSurveyInfo.setText(survey.getTxtButtonInformation()
//						.trim());
//				
//			} else
//			{
//				holder.btnSurveyInfo.setText("Information");
//			}
//
//		}

		// Show or hide Option buttons
		setupDetailIcons(survey, holder);

		// //////////////////////////////////
		if (surveys.get(position).getBitmapIcon() != null)
		{
			try
			{
				holder.ivStart.setImageBitmap(surveys.get(position)
						.getBitmapIcon());
			} catch (OutOfMemoryError e)
			{
				System.gc();
				Log.e(this.getClass().getSimpleName(),
						"Out of memory due too   app icon image");
				holder.ivStart.setImageDrawable(surveyScreen.getResources()
						.getDrawable(R.drawable.icon));

			}

		} else
		{
			holder.ivStart.setImageDrawable(surveyScreen.getResources()
					.getDrawable(R.drawable.icon));
		}

		// Listeners
		holder.startSurveyLayout.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				surveyScreen.onClick(new SurveyClick(R.id.btnStartSurvey,
						survey));
			}
		});

		holder.answerLayout.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				surveyScreen.onClick(new SurveyClick(R.id.btnAnswer, survey));
			}
		});

		holder.graphLayout.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				surveyScreen.onClick(new SurveyClick(R.id.btnSurveyResult,
						survey));
			}
		});

		holder.infoLayout.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				surveyScreen
						.onClick(new SurveyClick(R.id.btnSurveyInfo, survey));
			}
		});

		return convertView;

	}

	/**
	 * clear all Bitmap icons of survey list resources held by Survey List
	 */
	public void dispose()
	{
		super.dispose();

	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub

	}

}
